package demo.practice.binary_search;

public class Offer53 {

    public int search(int[] nums, int target) {
        if(nums==null| nums.length==0)
            return 0;
        int min = 0;
        int max = nums.length - 1;
        while (min <= max) {

            int mid = min + (max - min) / 2;
            if (nums[mid] == target) {
                //找到了目标值
                int po = mid;
                int ne = mid;
                int times = 1;
                while (po<nums.length - 1 &&  nums[++po] == target)
                    times++;

                while (ne>0 &&  nums[--ne] == target)
                    times++;

                return times;
            } else if (nums[mid] > target) {
                //目标值在 前面
                max = mid - 1;
            } else {
                //模板在后面
                min = mid + 1;
            }
        }

        return 0;

    }

}
